<?php
/*
 * @Author: CHINAKAIYUAN<sunkaiyuan@namenode.cn>
 * @Date: 2022-02-04 16:53:56
 * @LastEditTime: 2023-08-28 14:20:28
 * @LastEditors: 孙开源 && sunkaiyuan@namenode.cn
 * @Description: 加料单
 */

namespace App\Http\Services\Pro;


use App\Http\Services\Service;
use App\Repositories\EmployeeRepository;
use App\Repositories\ProEmployeeProRepository;
use App\Repositories\ProProDetailRepository;

/**
 * @property ProProDetailRepository $repository 
 * @property EmployeeRepository $employee 
 * @property ProEmployeeProRepository $salay_pro 
 */
class ProProDetailService extends Service
    {

    public function boot(ProProDetailRepository $repository, EmployeeRepository $employee, ProEmployeeProRepository $salay_pro)
        {
        $this->repository = $repository;
        $this->employee   = $employee;
        $this->salay_pro  = $salay_pro;
        }

    public function list()
        {
        // DB::enableQueryLog();
        $data = $this->repository->condition(request())->getGrid();
        // dd(DB::getQueryLog());
        return $data;
        }

    protected function save()
        {
        $request       = request();
        $data          = $request->toArray();
        $empoyee_model = $this->employee->whereId($request->swipor_id)->first();
        if (is_numeric($data['id'] ?? "") && $data['id'] > 0) {
            $gdata = $this->repository->whereId($data['id'])->first();
            $this->salay_pro->add($empoyee_model, 0 - $gdata['quantity'], $gdata['swiped_at']);
            }
        $data['position_id']    = $empoyee_model->position_id; //岗位
        $data['shift_id']       = $empoyee_model->shift_id; //参数
        $data['swiped_at']      = date("Y-m-d H:i:s");
        $data['process_pro_id'] = $this->salay_pro->add($empoyee_model, $data['quantity'], $data['swiped_at']);
        return $this->repository->autosave($data);
        }
    protected function delete()
        {
        return $this->repository->whereIn("id", $this->getRequestIds())->get()->filter(function ($model)
            {
            $this->salay_pro->add($model->swipor, 0 - $model['quantity'], $model['swiped_at']);
            $model->delete();
            });
        }
    }